//松手的时候循环所有组件，判断当前组件在哪一个之上，就插入到这个之前
import {getPageTree} from "../getPageTree";
export default function (Vue, page, com) {
    let layerY = parseInt(window.layerY);
    // let dom = document.querySelector(`div[data-gwdrag=${window.dragName}]`);
    // let container = document.querySelector(".container");
    let container = document.querySelector("#content-container");
    let components = container.querySelectorAll("div[data-gwdrag]");
    let exits = false;
    let area = "content";
    // console.log("这是layerY", layerY);
    for (let i = 0; i < components.length; i++) {
        // console.log(components[i].getBoundingClientRect().top);
        if (layerY <= components[i].getBoundingClientRect().top + 5) {
            // console.log("插入到这个元素之前", components[i]);
            insertDom(Vue, page, i, com, area);
            // console.log("+++++++++++++++++++++", i);
            exits = true;
            break;
        }
        // console.log("0000");
    }

    if (!exits) {
        // insertBefore(dom, components[i]);
        // console.log("插入到最后一个元素");
        insertDom(Vue, page, "new", com, area);
    }
}

function insertDom(Vue, page, index, com, area) {
    let domTree = Vue.$store.getters.getDomTree;
    //插入到最后一个
    if (index === "new") {
        let dom = {
            type: com,
            id: "xy" + parseInt(Math.random() * 10000),
            index: domTree[page][area].length,
            config: {}

        }
        domTree[page][area].push(dom);
        //更新domtree
        Vue.$store.commit("setDomTree", domTree);
        //更新配置项
        setEditConfigVuex(Vue, {type: com, id: dom.id});
        // console.log("更新后的", Vue.$store.getters.getDomTree.index);
        return;
    }
    domTree[page][area].forEach(item => {

        if (item.index >= index) {
            item.index = item.index + 1;
        }
    });
    let dom = {
        type: com,
        id: "xy" + parseInt(Math.random() * 10000),
        index,
        config: {}
    }
    domTree[page][area].push(dom);
    //更新domtree
    Vue.$store.commit("setDomTree", domTree);
    // console.log("更新后的", Vue.$store.getters.getDomTree.index);
    //更新配置项
    setEditConfigVuex(Vue, {type: com, id: dom.id});
}

//更新配置项
function setEditConfigVuex(context, obj) {
    context.$store.commit("setEditConfigVuex", obj);
}

function updateDom(Vue, page, index, com) {

}
